Decisions with if...else

Objectives


Discussion

Decision Making with if...else

If (a < b) then
   [Statements]
elseif (a = b) then
   [Statements]
else
   [Statements]
end if

Radio Buttons

CheckBoxes

Back to top


Demonstration

1.  We will now use radio buttons to allow the user to select the color for a form.  Add a new form (frmRadioButtons) to your unit3 project.

2.  Add a groupbox to the form and place two radio buttons (rdoBlue and rdoRed) in it.   Change the text property of the groupbox to "Color" and change the text properties of the radio buttons to "Blue" and "Red".  Change the checked property of rdoBlue to "True". This will make this button be checked when the form loads.

3.  Notice if you move the groupbox, the radio buttons will move with it. The radio buttons are grouped since the user can only choose one.  You should almost always put radio buttons in a group box. 

4.  Add a check box (chkEnabled) and change its text property to "Button Enabled".  Also change its checked property to "True" so that it is checked when the form loads.

5.  Add a button (btnChange) to the form.  Your form should look like:

     

6.  Now run the program and click on the various controls.

7.  Now we will add code so that the form changes to the selected color when the user clicks the button.  We will also give the user the option of disabling the button.  Add the following code to the click event for the button:

If rdoBlue.Checked = True Then
   Me.BackColor = Color.Blue
Else
   Me.BackColor = Color.Red
End If

8.  Notice in the above code we have used "color".  This is a built-in enumerated type.  Run the program and experiment.

9.  Now we will add code to make the checkbox work. We want the button to become disabled or enabled when the user clicks the checkbox.  We will use the CheckChanged event for the checkbox.  Either double-click the checkbox or use the drop-down boxes to open the CheckChanged event for the Checkbox.  Add the following code to this event:

If chkEnabled.Checked = True Then
   Me.btnChange.Enabled = True
Else
   Me.btnChange.Enabled = False
End If

10.  Run your program and check it out.

Back to top
Exercises

1.  Write a function that accepts a character.  If the character is an "A" or an "a" the function should return true otherwise the function should return false. 

2.  Create a login form that has a textbox for entering a userID and a textbox for entering a password. Make up a UserID and password for yourself.  If both the userID and password match your ID and password then open another form.  If the userID and password are not valid then clear the textboxes and set the cursor back into the UserID textbox.

3.  Create a form that looks the one shown in the figure below.  After the user enters a number in each textbox and clicks the Check button, the result of the comparison is displayed on the right, after the "=" sign.  The user is able to choose the type of operation using the checkbox and the radiobuttons.

      

Back to top
Links & Help
Back to top